package org.example.chapter4.aspect.controller;

import org.example.chapter4.aspect.service.UserService;
import org.example.chapter4.aspect.validator.UserValidator;
import org.example.chapter4.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author CoderXshuai
 * @date 2020/7/26/0026 18:26
 */
@Controller
@RequestMapping("/user")
public class UserController {
    //使用非接口注入
    @Autowired
    private UserService userService = null;

    //定义请求
    @RequestMapping("/print")
    //转换为JSON
    @ResponseBody
    public User printUser(Long id, String userName, String note) {
        User user = new User();
        user.setId(id);
        user.setUsername(userName);
        user.setNote(note);
        //若user==null,则执行afterThrowing方法
        userService.printUser(user);
        //加入断点
        return user;
    }

    //定义请求
    @RequestMapping("/vp")
    //返回JSON
    @ResponseBody
    public User validatorAndPrint(Long id, String userName, String note) {
        User user = new User();
        user.setId(id);
        user.setUsername(userName);
        user.setNote(note);
        //强制转换
        UserValidator userValidator = (UserValidator) userService;
        //验证用户是否为空
        if (userValidator.validate(user)) {
            userService.printUser(user);
        }
        return user;
    }

    @RequestMapping("/manyAspects")
    //返回JSON
//    @ResponseBody
    public String manyAspects() {
        userService.manyAspects();
        return "manyAspects";
    }
}
